package com.dreamfly.qiyeweixinrobot.task;

import com.alibaba.fastjson.JSONObject;
import com.dreamfly.qiyeweixinrobot.domain.ImageMessage;
import com.dreamfly.qiyeweixinrobot.domain.Message;
import com.dreamfly.qiyeweixinrobot.domain.TextMessage;
import com.dreamfly.qiyeweixinrobot.enums.MessageTypeEnum;
import com.dreamfly.qiyeweixinrobot.utils.ImageManager;
import com.dreamfly.qiyeweixinrobot.utils.MessageManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @description: 日报提醒和分享任务
 * @author: DreamflyChen
 * @create: 2020-08-29 21:51
 */
@Component
public class DailyTipAndShareTask {

    @Scheduled(cron = "0 0 10 ? * 1")
    public void ShareTip() throws Exception {
        String webHookAddress = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=...";
        //发送文本消息
        Message message = new Message();
        message.setMsgtype(MessageTypeEnum.text);
        TextMessage textMessage = new TextMessage();
        textMessage.setContent("这里写文本消息内容。");
        textMessage.setMentioned_list(Arrays.asList("user id list"));
        JSONObject text = JSONObject.parseObject(JSONObject.toJSONString(textMessage));
        message.setText(text);
        MessageManager.sendMessage(message, webHookAddress);

        //发送图片消息
        message.setText(null);
        message.setMsgtype(MessageTypeEnum.image);
        ImageMessage imageMessage = new ImageMessage();
        imageMessage.setBase64(ImageManager.imageToBase64Str("/img/朱茵.jpg"));
        imageMessage.setMd5(ImageManager.getStringMd5("/img/朱茵.jpg"));
        JSONObject image = JSONObject.parseObject(JSONObject.toJSONString(imageMessage));
        message.setImage(image);
        MessageManager.sendMessage(message, webHookAddress);
    }

    public static void main(String[] args) throws Exception {
        new DailyTipAndShareTask().ShareTip();
    }
}
